package com.agu.mgt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;
import com.agu.entity.sys.SysMenuEntity;
import com.agu.mgt.sys.dto.MenuNode;
import com.agu.mgt.sys.service.SysMenuService;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.FormatUtil;
import com.agu.utils.JsonUtils;
import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author
 * @email
 * @date 2018-10-10 16:31:20
 */
@SuppressWarnings("rawtypes")
@RestController
@Api("")
@RequestMapping("/sys/menu")
public class SysMenuController extends AbstractController {
	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("sys:menu:list")
	public Result list(@RequestParam(value="plat", defaultValue="1") int plat) {

		// 查询列表数据
		List<SysMenuEntity> list = sysMenuService.getAllMenuList(plat);
		
		List<MenuNode> nodelist = new ArrayList<MenuNode>();
		
		for(SysMenuEntity menu:list)
		{
			MenuNode node = new MenuNode(menu);
			nodelist.add(node);
		}
			
		List<MenuNode> menuList = MenuNode.buildTree(nodelist);
		
		Map map = new HashMap();
		map.put("menuList", menuList);
		
		return ResultGenerator.successData(map);
	}

	/**
	 * 信息
	 */
	@GetMapping("/info/{menuId}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("sys:menu:info")
	public Result info(@PathVariable("menuId") Long menuId) {
		SysMenuEntity sysMenu = sysMenuService.queryObject(menuId);

		if(sysMenu!=null &&sysMenu.getParentId()==0)
		{
			sysMenu.setParentName("一级菜单");
		}
		
		return ResultGenerator.successData(sysMenu);
	}
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:menu:select")
	public Result select(@RequestParam(value="plat", defaultValue="1") int plat) {
		
		// 查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList(plat);

		// 添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return ResultGenerator.successData(menuList);
	}
	/**
	 * 保存
	 */
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("sys:menu:save")
	public Result save(@RequestBody SysMenuEntity sysMenu) {
		
		
		
		sysMenuService.save(sysMenu);

		return ResultGenerator.success("保存成功");
	}

	/**
	 * 排序 
	 */
	@SysLog("菜单排序")
	@PostMapping("/sort")
	@ApiOperation(value = "排序", notes = "说明")
	@RequiresPermissions("sys:menu:update")
	public Result sort(@RequestBody ArrayList<HashMap> list) {
		
		sysMenuService.updateSort(list);
		
		//记录日志
		String json = JsonUtils.toJson(list);
		//String remark = FormatUtil.formatJson(json);
		
		return ResultGenerator.success("排序成功").setRemark(json);
	}
	
	
	/**
	 * 修改
	 */
	@SysLog("修改菜单")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("sys:menu:update")
	public Result update(@RequestBody SysMenuEntity sysMenu) {
		
		SysMenuEntity sysMenu1 = sysMenuService.queryObject(sysMenu.getMenuId());
		
		sysMenuService.update(sysMenu);

		Map<String, String> cmap = new HashMap<String, String>();

		cmap.put("menuId", "菜单ID");
		cmap.put("parentId", "父菜单ID");
		cmap.put("name", "菜单名称");
		cmap.put("url", "菜单URL");
		cmap.put("perms", "授权");
		cmap.put("type", "类型");
		cmap.put("icon", "菜单图标");
		cmap.put("orderNum", "排序");
		String remark = CompareUtils.getTwoObjectRemark(sysMenu1, sysMenu, cmap);
		
		return ResultGenerator.success("修改成功").setRemark(remark);
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("sys:menu:delete")
	public Result delete(@RequestBody Long[] menuIds) {
		sysMenuService.deleteBatch(menuIds);

		return ResultGenerator.success("删除成功");
	}

	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("sys:menu:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);

		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<SysMenuEntity> list = sysMenuService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

			Map<Integer, String> typeMap = new HashMap<Integer, String>();

			typeMap.put(0, "目录");
			typeMap.put(1, "菜单");
			typeMap.put(2, "按钮");
			map.put("typeMap", typeMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

			cloumn = new Column("菜单ID", "menuId", 1);
			clist.add(cloumn);
			cloumn = new Column("父菜单ID", "parentId", 1);
			clist.add(cloumn);
			cloumn = new Column("菜单名称", "name", 1);
			clist.add(cloumn);
			cloumn = new Column("菜单URL", "url", 1);
			clist.add(cloumn);
			cloumn = new Column("授权", "perms", 1);
			clist.add(cloumn);
			cloumn = new Column("类型", "type", 2);
			clist.add(cloumn);
			cloumn = new Column("菜单图标", "icon", 1);
			clist.add(cloumn);
			cloumn = new Column("排序", "orderNum", 1);
			clist.add(cloumn);
			cloumn = new Column("平台", "plat", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					// this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
